package com.lagou.es.controller;

import com.lagou.es.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class PositionController {
    
    @Autowired
    private PositionService positionService;

    /**
     * 首页
     * @return 首页路径
     */
    @GetMapping({"/","/index"})
    public String index(){
        return "index";
    }

    /**
     * 分页检索职位信息
     * @param keyword
     * @param pageNo
     * @param pageSize
     * @return 精确查找的职位信息
     */
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    @ResponseBody
    public List<Map<String,Object>> searchPosition(@PathVariable("keyword") String  keyword, 
                                                   @PathVariable("pageNo")int pageNo,
                                                   @PathVariable("pageSize")int  pageSize) {
        String field = "positionName";
        List<Map<String,Object>> list = positionService.searchPos(keyword,field, pageNo, pageSize);
        return  list;
    }

    /**
     * 根据词条过滤推荐职位
     * @param terms 过滤词条
     * @param pageNo 页码
     * @param pageSize 页容量
     * @return 词条过滤推荐的职位信息
     */
    @GetMapping("/search/terms/{terms}/{pageNo}/{pageSize}")
    @ResponseBody
    public List<Map<String,Object>> positionAdvantage(@PathVariable("terms") String  terms,
                                                      @PathVariable("pageNo")int pageNo,
                                                      @PathVariable("pageSize")int  pageSize) {
        String field = "positionAdvantage";
        List<Map<String,Object>> list = positionService.searchPos(terms,field,pageNo,pageSize);
        return list;
    }

    /**
     * 将mysql中到数据导入到ES中
     * @return
     */
    @RequestMapping("/importAll")
    @ResponseBody
    public   String  importAll(){
        positionService.importAll();
        return  "success";
    }
}
